#include <iostream>
#include <vector>
#include <queue>
using namespace std;

int main()
{
	int n;
	cin >> n;

	vector<vector<int>> gh(n, vector<int>(n, 0));
	vector<int> vis(n, 0);
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			cin >> gh[i][j];
		}
	}

	auto cmp = [](const pair<int, int> &a, const pair<int, int> &b) { return a.second > b.second; };
	priority_queue<pair<int, int>, vector<pair<int, int>>, decltype(cmp)> pq(cmp);

	pq.push({ 0, 0 });
	int ans = 0;
	while (!pq.empty()) {
		auto e = pq.top();
		pq.pop();

		int u = e.first;
		if (vis[u] == 0) {
			ans += e.second;
			vis[u] = 1;
			for (int v = 0; v < n; v++) {
                                if(gh[u][v]){
				        pq.push({ v, gh[u][v] });
                                }
			}
		}
	}

	cout << ans << endl;
	return 0;
}